package com.gadgets.bm10;

import com.gadgets.bm10.common.dto.CopyArgDTO;

import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;

public class GeneratorCopyCode {

    private static final CopyArgDTO source;

    private static final CopyArgDTO target;

    static {
        // 原类名、对象属性名
        source = new CopyArgDTO("com.gadgets.bm10.common.Ck3ModifyDict","ck3ModifyDTO");
        // 目标类名、对象属性名
        target = new CopyArgDTO("com.gadgets.bm10.common.Ck3ModifyDictAddDTO","dict");
    }

    public static void main(String[] args) {
        try {
            // 获取Class对象
            Class sourceClazz = Class.forName(source.getClazzName());
            Class targetClazz = Class.forName(target.getClazzName());
            // 读取属性及其类型
            HashMap<String,Field> sourceField = generatorFieldName(sourceClazz);
            HashMap<String,Field> targetField = generatorFieldName(targetClazz);
            StringBuilder builder = new StringBuilder();
            for (Map.Entry<String, Field> entry : targetField.entrySet()) {
                if(sourceField.containsKey(entry.getKey())){
                    // 如果属性存在，比较类型是否一致
                    if(entry.getValue().getType()==sourceField.get(entry.getKey()).getType()){
                        String FuncName = entry.getValue().getName();
                        char[] chars = FuncName.toCharArray();
                        chars[0]-=32;
                        FuncName = String.valueOf(chars);
                        builder.append(target.getPropName()).append(".").append("set").append(FuncName);
                        builder.append("(");
                        builder.append(source.getPropName()).append(".").append("get").append(FuncName).append("()");
                        builder.append(");\n");
                        targetField.put(entry.getKey(), null);
                        sourceField.put(entry.getKey(), null);
                    }else{
                        System.err.println("属性："+entry.getKey()+"存在，但数据类型不一致");
                    }
                }
            }
            System.out.println("------------------------------");
            System.out.println(builder.toString());
            System.err.println("------------------------------");
            System.err.println("target未赋值属性：");
            printNoCopyParam(targetField);
            System.err.println("------------------------------");
            System.err.println("source未取值属性：");
            printNoCopyParam(sourceField);
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        }
    }

    private static void printNoCopyParam(HashMap<String, Field> sourceField) {
        for (String paramName : sourceField.keySet()) {
            if(null!= sourceField.get(paramName)) {
                System.err.println("属性：" + paramName);
            }
        }
    }

    private static HashMap<String,Field> generatorFieldName(Class targetClazz) {
        HashMap<String,Field> tagetField = new HashMap<>();
        for (Field field : targetClazz.getDeclaredFields()) {
            tagetField.put(field.getName(), field);
        }
        return tagetField;
    }

}
